package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QQSMSUtils;
import com.ruoyi.system.domain.TalCuptureNotice;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITalCuptureNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TalNoticePaymentMapper;
import com.ruoyi.system.domain.TalNoticePayment;
import com.ruoyi.system.service.ITalNoticePaymentService;

/**
 * 党费催缴Service业务层处理
 *
 * @author anxinm
 * @date 2024-04-07
 */
@Slf4j
@Service
public class TalNoticePaymentServiceImpl extends ServiceImpl<TalNoticePaymentMapper ,TalNoticePayment> implements ITalNoticePaymentService
{
    @Autowired
    private TalNoticePaymentMapper talNoticePaymentMapper;

    @Autowired
    private ITalCuptureNoticeService talCuptureNoticeService;

    @Autowired
    private ISysUserService userService ;

    /**
     * 查询党费催缴
     *
     * @param id 党费催缴主键
     * @return 党费催缴
     */
    @Override
    public TalNoticePayment selectTalNoticePaymentById(Long id)
    {
        return talNoticePaymentMapper.selectTalNoticePaymentById(id);
    }

    /**
     * 查询党费催缴列表
     *
     * @param talNoticePayment 党费催缴
     * @return 党费催缴
     */
    @Override
    public List<TalNoticePayment> selectTalNoticePaymentList(TalNoticePayment talNoticePayment)
    {
        return talNoticePaymentMapper.selectTalNoticePaymentList(talNoticePayment);
    }

    /**
     * 新增党费催缴
     *
     * @param talNoticePayment 党费催缴
     * @return 结果
     */
    @Override
    public int insertTalNoticePayment(TalNoticePayment talNoticePayment)
    {
        talNoticePayment.setCreateTime(DateUtils.getNowDate());
        if (ObjectUtils.isEmpty(talNoticePayment.getUserId())) {
            throw new ServiceException("通知党员不能为空!") ;
        }
        if (ObjectUtils.isEmpty(talNoticePayment.getNoticeId())) {
            throw new ServiceException("党费收缴通知不能为空!") ;
        }

        return talNoticePaymentMapper.insert(talNoticePayment);
    }

    /**
     * 修改党费催缴
     *
     * @param talNoticePayment 党费催缴
     * @return 结果
     */
    @Override
    public int updateTalNoticePayment(TalNoticePayment talNoticePayment)
    {
        talNoticePayment.setUpdateTime(DateUtils.getNowDate());
        return talNoticePaymentMapper.updateById(talNoticePayment);
    }

    /**
     * 批量删除党费催缴
     *
     * @param ids 需要删除的党费催缴主键
     * @return 结果
     */
    @Override
    public int deleteTalNoticePaymentByIds(Long[] ids)
    {
        return talNoticePaymentMapper.deleteTalNoticePaymentByIds(ids);
    }

    /**
     * 删除党费催缴信息
     *
     * @param id 党费催缴主键
     * @return 结果
     */
    @Override
    public int deleteTalNoticePaymentById(Long id)
    {
        return talNoticePaymentMapper.deleteTalNoticePaymentById(id);
    }

    @Override
    public void updateState(TalNoticePayment payment) {
        if (ObjectUtils.isEmpty(payment.getId())) {
            throw new ServiceException("修改信息主键不能为空!") ;
        }

        if (ObjectUtils.isEmpty(payment.getIsPayment()) && ObjectUtils.isEmpty(payment.getIsNotice())) {
            throw new ServiceException("状态不能为空!") ;
        }

        if (ObjectUtils.isNotEmpty(payment.getIsNotice()) && payment.getIsNotice() == 1L ) {
            /**收缴信息*/
            TalNoticePayment noticePayment = this.getById(payment.getId());
            /**收缴通知*/
            TalCuptureNotice notice = talCuptureNoticeService.getById(noticePayment.getNoticeId());
            /**用户信息*/
            LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysUser::getUserId ,noticePayment.getUserId()) ;
            SysUser sysUser = userService.getOne(wrapper);
            /**发送邮箱通知*/
            try {
                QQSMSUtils.noticeTalentEmail(sysUser.getEmail() ,notice.getTitle() ,notice.getContent());
            } catch (Exception e) {
                e.printStackTrace();
                log.info(e.getMessage());
                throw new ServiceException("催缴失败，请联系管理员!") ;
            }
        }

        this.updateById(payment) ;
    }
}
